package shiyan;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MsgFrame extends JFrame implements ActionListener,KeyListener{
    //定义控件
    JLabel lab1,lab2,lab3;
    JTextArea taContent;
    JComboBox cbExp;
    JTextField txtMsg;
    JButton btnSubmit,btnClear,btnTop,btnBottom,btnShow;
    //构造方法
    public MsgFrame() {
        super("小小留言板");
        this.setSize(800,600);
        this.winInit();
    }
    //组件初始化
    public void winInit(){
        lab1=new JLabel("留言板",0);
        lab2=new JLabel("你");
        lab3=new JLabel("地说：");
        taContent=new JTextArea("留言内容:\n");
        taContent.setEditable(false);
        cbExp=new JComboBox();
        cbExp.addItem("微笑");
        cbExp.addItem("大哭");
        cbExp.addItem("流泪");
        this.loadExp();
        txtMsg=new JTextField(35);
        btnSubmit=new JButton("提交");
        btnClear=new JButton("清屏");
        btnTop=new JButton("至顶");
        btnBottom=new JButton("至尾");
        btnShow=new JButton("查看");
        btnSubmit.addActionListener(this);
        txtMsg.addKeyListener(this);
        btnClear.addActionListener(this);
        btnTop.addActionListener(this);
        btnBottom.addActionListener(this);
        btnShow.addActionListener(this);
        //中间滚动面板
        JScrollPane scroll=new JScrollPane(taContent);
        //南方面板
        JPanel pSouth=new JPanel();
        pSouth.add(lab2);
        pSouth.add(cbExp);
        pSouth.add(lab3);
        pSouth.add(txtMsg);
        pSouth.add(btnSubmit);
        //东方面板
        JPanel pEast=new JPanel();
        pEast.setLayout(new GridLayout(8,1,0,3));
        pEast.add(btnClear);
        pEast.add(btnTop);
        pEast.add(btnBottom);
        pEast.add(btnShow);
        add(lab1,"North");
        add(pSouth,"South");
        add(scroll,"Center");
        add(pEast,"East");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    //加载文件中的表情
    public void loadExp(){
        cbExp.removeAllItems();
        try {
            BufferedReader br = new BufferedReader(new FileReader("Expression.dat"));
            String exp="";
            while ((exp=br.readLine())!=null){
                cbExp.addItem(exp);
            }
            br.close();
        }catch (IOException e1){
            System.out.println("读取文件失败"+e1);
        }
    }
    //加载文件内容
    public void loadMsg(){
        taContent.setText("留言内容:\n");
        try {
            BufferedReader br = new BufferedReader(new FileReader("Msg.dat"));
            String msg="";
            while ((msg=br.readLine())!=null){
                taContent.insert(msg+"\n",6);
            }
            br.close();
        }catch (IOException e1){
            System.out.println("读取文件失败"+e1);
        }
    }
    //提交留言的方法
    public void msgSubmit(){
        if (txtMsg.getText().length()==0) return;
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr=df.format(new Date());
        String msgInfo="["+dateStr+"]"+"你"+cbExp.getSelectedItem()+"地说"+txtMsg.getText();
        taContent.insert(msgInfo+"\n",6);
        txtMsg.setText("");
        //写入文件Msg.dat
        File f=new File("Msg.dat");
        try{
            FileWriter fw=new FileWriter(f,true);
            fw.write(msgInfo+"\r");
            fw.close();
        }catch (IOException e1){
            System.out.println("写文件失败"+e1);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().equals("提交")){
            msgSubmit();
        }else if (e.getActionCommand().equals("清屏")){
            taContent.setText("留言内容:\n");
        }else if (e.getActionCommand().equals("至顶")){
            taContent.setCaretPosition(0);
        }else if (e.getActionCommand().equals("至尾")){
            taContent.setCaretPosition(taContent.getText().length()-1);
        }else if (e.getActionCommand().equals("查看")){
            this.loadMsg();
        }
    }
    @Override
    public void keyTyped(KeyEvent keyEvent) {

    }
    @Override
    public void keyReleased(KeyEvent keyEvent) {

    }
    @Override
    public void keyPressed(KeyEvent e){
        if (e.getKeyCode()==10){
            msgSubmit();
        }
    }
    public static void main(String[] args) {
        MsgFrame ms=new MsgFrame();
        ms.setVisible(true);
    }
}

